/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.datastore.database.querybuilder;

import at.schauer.gregor.vienna.datastore.StorageMetadata;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Gregor Schauer
 */
public class NamedQueryBuilder implements QueryBuilder {
	private static final Object[] EMPTY_OBJECTS = new Object[0];
	private static final String[] EMPTY_STRINGS = new String[0];
	private static final Class[] EMPTY_CLASSES = new Class[0];
	protected Map<String, NamedQuery> queryMap = new HashMap<String, NamedQuery>();



	public NamedQueryBuilder(Class<?>... classes) {
		register(classes);
	}

	@Override
	public String getQuery(String key) {
		NamedQuery namedQuery = getNamedQuery(key);
		return namedQuery.query();
	}

	@Override
	public Object[] getParameters(String key, StorageMetadata<?> metadata) {
		if (READ.equals(key) || DELETE.equals(key)) {
			return new Object[] {metadata.identifier};
		}
		return EMPTY_OBJECTS;
	}

	@Override
	public Object[] getParameters(String key, Map<String, Object> map) {
		String[] parameterNames = getParameterNames(key);
		Object[] parameters = new Object[parameterNames.length];
		for (int i = 0; i < parameterNames.length; i++) {
			parameters[i] = map.get(parameterNames[i]);
		}
		return parameters;
	}

	@Override
	public String[] getParameterNames(String key) {
		NamedQuery namedQuery = getNamedQuery(key);
		for (QueryHint hint : namedQuery.hints()) {
			if (PARAMETER_NAMES.equals(hint.name())) {
				return StringUtils.delimitedListToStringArray(hint.value(), ",", " ");
			}
		}
		return EMPTY_STRINGS;
	}

	protected NamedQuery getNamedQuery(String key) {
		NamedQuery query = queryMap.get(key);
		if (query == null) {
			throw new RuntimeException(key);
		}
		return query;
	}

	protected NamedQueries getNamedQueries(Class<?> clazz) {
		NamedQueries namedQueries = clazz.getAnnotation(NamedQueries.class);
		return namedQueries != null ? namedQueries : null;
	}

	public void register(Class<?>... classes) {
		for (Class<?> clazz : ObjectUtils.defaultIfNull(classes, EMPTY_CLASSES)) {
			NamedQueries namedQueries = getNamedQueries(clazz);
			register(namedQueries);
		}
	}

	public void register(NamedQueries namedQueries) {
		if (namedQueries != null) {
			for (NamedQuery namedQuery : namedQueries.value()) {
				queryMap.put(namedQuery.name(), namedQuery);
			}
		}
	}
}
